<?php

/**
 * This is the model class for table "tbl_banner".
 *
 * The followings are the available columns in table 'tbl_banner':
 * @property integer $id
 * @property string $title
 * @property string $position
 * @property integer $is_active
 * @property string $source_file
 * @property string $date_out
 */
class Banner extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Banner the static model class
	 */

    public static $positions = array(
        'wide_top' => 'По центру верх',
        'wide_middle' => 'По центру середина',
        'left_section_1' => 'Слева 1',
        'left_section_2' => 'Слева 2',
        'left_section_3' => 'Слева 3',
        'left_section_4' => 'Слева 4',
        'left_section_5' => 'Слева 5',
        'left_section_6' => 'Слева 6',
    );

    public $file;

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_banner';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, position, date_out', 'required'),
			array('is_active', 'numerical', 'integerOnly'=>true),
			array('title, source_file', 'length', 'max'=>255),
			array('position', 'length', 'max'=>14),
            array('file', 'file', 'types'=>'jpg, gif, png','on'=>'add'),
            array('file', 'file', 'types'=>'jpg, gif, png','allowEmpty'=>true, 'on'=>'mnf'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, position, is_active, source_file, date_out', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Название',
			'position' => 'Положение',
			'is_active' => 'Активен',
			'source_file' => 'Файл',
			'date_out' => 'Дата отключения',
            'file' => 'Файл'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('position',$this->position,true);
		$criteria->compare('is_active',$this->is_active);
		$criteria->compare('source_file',$this->source_file,true);
		$criteria->compare('date_out',$this->date_out,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    protected function beforeSave()
    {
     $this->date_out = date('Y-m-d H:i:s', strtotime($this->date_out));
        return true;
    }
}